package com.zsk.common;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 
 * @author zincredible
 * @date 2019/04/03 13:24:02
 */
public class CommonUtil {
	private final static DateFormat YMDHMS_FORMAT = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	private final static DecimalFormat TWOPOINT_FORMAT = new DecimalFormat("#.00");

	public static String getCurrentTime() {
		return YMDHMS_FORMAT.format(new Date());
	}

	public static boolean isNull(Object o) {
		return o == null ? true : false;
	}

	public static boolean isNull(String o) {
		return o == null || o.trim().equals("") ? true : false;
	}

	/**
	 * double 保留两位小数
	 * 
	 * @param num
	 * @return
	 */
	public static double getTwoPointNum(double num) {
		String str = TWOPOINT_FORMAT.format(num);
		double temp = Double.valueOf(str);
		return temp;
	}

	/**
	 * 将文件字节数转换成可读的MB或KB
	 * 
	 * @param size
	 * @return
	 * @author: zhangsike
	 * @date: 2018/11/17 17:20:31
	 */
	public static String getFileSize(long size) {
		String fileSize = "";
		if (size > 1024 * 1024) {
			double lenght = size / 1024.0 / 1024.0;
			fileSize = CommonUtil.getTwoPointNum(lenght) + "MB";
			return fileSize;
		}
		double lenght = size / 1024.0;
		fileSize = CommonUtil.getTwoPointNum(lenght) + "KB";
		return fileSize;
	}

	/**
	 * 
	 * @description: 获取分页PageBable对象
	 * @param pageIndex    页面索引 从1开始
	 * @param pageSize     页面大小，最大不超过100条
	 * @param totalSize    总页面
	 * @param sort         排序
	 * @param pageResponse 页面对象
	 * @return
	 * @author: zhangsike
	 * @date: 2018/11/17 17:12:32
	 */
//	public static Pageable getPageRequest(int pageIndex, int pageSize, long totalSize, Sort sort,
//			PageBean<?> pageResponse) {
//		if (pageSize > 100) {
//			pageSize = 100;
//		} else if (pageIndex < 1) {
//			pageIndex = 1;
//		}
//		int totalPageNum = (int) Math.ceil(totalSize * 1.0 / pageSize);
//		if (totalPageNum > 0 && pageIndex > totalPageNum) {
//			pageIndex = totalPageNum;
//		}
//		pageResponse.setPageCount(totalPageNum);
//		pageResponse.setPageIndex(pageIndex);
//		Pageable request = new PageRequest(pageIndex - 1, pageSize, sort);
//		return request;
//	}
//
//	public static Sort getDescSort(String k) {
//		return new Sort(Direction.DESC, k);
//	}
//
//	public static Sort getAscSort(String k) {
//		return new Sort(Direction.ASC, k);
//	}

	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

}
